import 'package:merchants/models/spu.dart';

class SpuListResponse {
  final int page;
  final int pageSize;
  final int total;
  final List<Spu> records;

  SpuListResponse({
    required this.page,
    required this.pageSize,
    required this.total,
    required this.records,
  });

  factory SpuListResponse.fromJson(Map<String, dynamic> json) {
    return SpuListResponse(
      page: json['page'] as int? ?? 1,
      pageSize: json['pageSize'] as int? ?? 20,
      total: json['total'] as int? ?? 0,
      records: (json['records'] as List<dynamic>?)
          ?.map((item) => Spu.fromJson(item as Map<String, dynamic>))
          .toList() ?? [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'page': page,
      'pageSize': pageSize,
      'total': total,
      'records': records.map((spu) => spu.toJson()).toList(),
    };
  }

  // 计算总页数
  int get totalPages => (total / pageSize).ceil();

  // 是否有下一页
  bool get hasNextPage => page < totalPages;

  // 是否有上一页
  bool get hasPreviousPage => page > 1;

  // 获取当前页的数据范围
  String get pageInfo => '第 $page 页，共 $totalPages 页，总计 $total 条记录';
} 